/*
 * Copyright 2012 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.db.obj.vis.domain.domains;

import com.google.common.collect.Lists;
import java.util.ArrayList;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.ElementListUnion;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.core.Commit;

/**
 *
 * @author yura
 */
@Root
public class ObjectEntry extends AbstractEntity<ObjectPriority, ObjectId> {

    @ElementListUnion({
        @ElementList(name="simpleAtribute", inline=true, type=SimpleAtribute.class),
        @ElementList(name="manyToManyAttribute", inline=true, type=ManyToManyAttribute.class),
        @ElementList(name="objectAttribute", inline=true, type=ObjectAttribute.class)
    })
    private ArrayList<AbstractAttributeEntry> attributes = Lists.newArrayList();

    public ObjectEntry() {
    }

    public ObjectEntry(ObjectId objectId) {
        this.id = objectId;
    }

    public ArrayList<AbstractAttributeEntry> getAttributes() {
        return attributes;
    }

    public void addAttribute(AbstractAttributeEntry attributeEntry) {
        getAttributes().add(attributeEntry);
    }

    public void setAttributes(ArrayList<AbstractAttributeEntry> attributes) {
        this.attributes = attributes;
    }
    
    @Commit
    public void commit() {
        for (ObjectPriority priority : prioritys) {
            priority.getId().setEntity(this);
        }
        
        for (AbstractAttributeEntry attribute : attributes) {
            attribute.getId().setObjectEntry(this);
            for (AttributePriority priority : attribute.getPrioritys()) {
                priority.getId().setEntity(attribute);
            }
        }
    }
    
    @Override
    public String toString() {
        return "ObjectEntry{" + id + "description=" + description 
                + ", prioritys=" + prioritys + "attributes" + attributes + '}';
    }
}
